<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
<head>
  <meta content="text/html; charset=ISO-8859-1"  http-equiv="content-type" />
  <link rel="stylesheet" type="text/css" href="style.css" />
  <title>SOCI - errors</title>
</head>

<body>
<p class="banner">SOCI - The C++ Database Access Library</p>

<h2>Errors</h2>

<p>All DB-related errors manifest themselves as exceptions of type <code>soci_error</code>,
which is derived from <code>std::runtime_error</code>.<br />
This allows to
handle database errors within the standard exception framework:</p>

<pre class="example">
int main()
{
    try
    {
        // regular code
    }
    catch (exception const &amp; e)
    {
        cerr &lt;&lt; "Bang! " &lt;&lt; e.what() &lt;&lt; endl;
    }
}
</pre>

<div class="note">
<p><span class="note">Portability note:</span></p>
<p>The Oracle backend can also throw the instances of the <code>oracle_soci_error</code>,
which is publicly derived from <code>soci_error</code> and has an
additional public <code>err_num_</code>
member containing the Oracle error code:</p>

<pre class="example">
int main()
{
    try
    {
        // regular code
    }
    catch (oracle_soci_error const &amp; e)
    {
        cerr &lt;&lt; "Oracle error: " &lt;&lt; e.err_num_
            &lt;&lt; " " &lt;&lt; e.what() &lt;&lt; endl;
    }
    catch (exception const &amp; e)
    {
        cerr &lt;&lt; "Some other error: " &lt;&lt; e.what() &lt;&lt; endl;
    }
}
</pre>
</div>

<div class="note">
<p><span class="note">Portability note:</span></p>
<p>The MySQL backend can throw instances of the <code>mysql_soci_error</code>,
which is publicly derived from <code>soci_error</code> and has an
additional public <code>err_num_</code>
member containing the MySQL error code (as returned by
<code>mysql_errno()</code>):</p>

<pre class="example">
int main()
{
    try
    {
        // regular code
    }
    catch (mysql_soci_error const &amp; e)
    {
        cerr &lt;&lt; "MySQL error: " &lt;&lt; e.err_num_
            &lt;&lt; " " &lt;&lt; e.what() &lt;&lt; endl;
    }
    catch (exception const &amp; e)
    {
        cerr &lt;&lt; "Some other error: " &lt;&lt; e.what() &lt;&lt; endl;
    }
}
</pre>
</div>

<table class="foot-links" border="0" cellpadding="2" cellspacing="2">
  <tr>
    <td class="foot-link-left">
      <a href="structure.html">Previous (Library structure, files and compilation)</a>
    </td>
    <td class="foot-link-right">
      <a href="basics.html">Next (Connections and simple queries)</a>
    </td>
  </tr>
</table>

<p class="copyright">Copyright &copy; 2004-2008 Maciej Sobczak, Stephen Hutton</p>
</body>
</html>
